use std::{iter::once, vec};

use swc_ecma_utils::quote_expr;
use crate::descriptor::field_descriptor_proto::Label;
use swc_common::DUMMY_SP;
use swc_ecma_ast::*;
use swc_ecma_ast::{MemberExpr, MemberProp, IfStmt, ReturnStmt, Lit,
    Accessibility, BinaryOp, Class, ClassMember, ClassMethod, Decl, Decorator, ExportDecl, Expr,
    MethodKind, ModuleDecl, ModuleItem, Param, PropName, Stmt,
    TsEntityName, TsExprWithTypeArgs, TsType, TsTypeAnn, TsTypeRef,
};
use heck::{ToLowerCamelCase, ToUpperCamelCase};
use swc_ecma_utils::quote_ident;
use crate::{
    descriptor::{
        self, field_descriptor_proto::Type, DescriptorProto, FieldDescriptorProto,
        OneofDescriptorProto,
    },
    context::{Context, PropPos, Syntax},
};

use super::{
    common::{normalize_name, normalize_type_name, ns_name},
    LarkPbRuntime,
};

impl LarkPbRuntime {
    pub(super) fn print_from_object(
        &self,
        ctx: &mut Context,
        descriptor: &DescriptorProto,
        ns: &[String],
    ) -> ModuleItem {
        let lark = ctx.get_import(Self::LARK_PACKAGE, Some(Self::LARK_PACKAGE_NAME));
        let interface_type = ctx.lazy_decl_type_ref(&ns_name(ns, descriptor.name()), PropPos::Interface);
        let instance_type = ctx.lazy_decl_type_ref(&ns_name(ns, descriptor.name()), PropPos::Definition);
    
        // 实现 FromObject 接口
        let implement = TsExprWithTypeArgs {
            span: DUMMY_SP,
            expr: Box::new(crate::member_expr!(lark, "FromObject")),
            type_args: Some(Box::new(swc_ecma_ast::TsTypeParamInstantiation {
                span: DUMMY_SP,
                params: vec![
                    Box::new(TsType::TsTypeRef(TsTypeRef {
                        span: DUMMY_SP,
                        type_name: TsEntityName::Ident(interface_type.clone()),
                        type_params: None,
                    })),
                    Box::new(TsType::TsTypeRef(TsTypeRef {
                        span: DUMMY_SP,
                        type_name: TsEntityName::Ident(instance_type.clone()),
                        type_params: None,
                    })),
                ],
            })),
        };
    
        // 动态生成字段适配逻辑
        let mut props = vec![];
        
        for oneof in &descriptor.oneof_decl {//处理oneof字段
            let oneof_ident = quote_ident!(normalize_name(oneof.name()));
            let mut ns = ns.to_vec();
            ns.push(descriptor.name().to_string());
            let oneof_type = crate::type_ref!(crate::entity_name_ident!(
                ctx.lazy_type_ref(&ns_name(&ns, &oneof.name().to_upper_camel_case()))
            ));
            // let ty = crate::type_union!(oneof_type, crate::undefined_type!());
            props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
                key: PropName::Ident(oneof_ident.clone()),
                value: Box::new(Expr::Member(MemberExpr {
                    span: DUMMY_SP,
                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                    prop: MemberProp::Ident(oneof_ident),
                })),
            }))));
            continue;
        }

        for field in &descriptor.field {
            if !field.has_oneof_index() {
                let field_name_ident = quote_ident!(normalize_name(field.name()));
            
                // Generate the type conversion expression
                let conversion_expr = if field.is_repeated() {
                    if !field.is_map(&ctx) {// Handle repeated fields
                        let element_conversion = match field.type_() {
                            Type::TYPE_STRING => {
                                Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Ident(quote_ident!("item"))),
                                        op: op!("!="),
                                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Ident(quote_ident!("item"))),
                                            op: op!("!=="),
                                            right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                        })),
                                        cons: Box::new(Expr::Cond(CondExpr {
                                            span: DUMMY_SP,
                                            test: Box::new(Expr::Bin(BinExpr {
                                                span: DUMMY_SP,
                                                left: Box::new(Expr::Unary(UnaryExpr {
                                                    span: DUMMY_SP,
                                                    op: op!("typeof"),
                                                    arg: Box::new(Expr::Ident(quote_ident!("item"))),
                                                })),
                                                op: op!("=="),
                                                right: Box::new(Expr::Lit(Lit::Str(Str {
                                                    span: DUMMY_SP,
                                                    value: "string".into(),
                                                    raw: None,
                                                }))),
                                            })),
                                            cons: Box::new(Expr::Ident(quote_ident!("item"))),
                                            alt: Box::new(Expr::Call(CallExpr {
                                                span: DUMMY_SP,
                                                callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("String")))),
                                                args: vec![ExprOrSpread {
                                                    spread: None,
                                                    expr: Box::new(Expr::Ident(quote_ident!("item"))),
                                                }],
                                                type_args: None,
                                            })),
                                        })),
                                        alt: Box::new(Expr::Lit(Lit::Str(Str {
                                            span: DUMMY_SP,
                                            value: "".into(),
                                            raw: None,
                                        }))),
                                    })),
                                    alt: Box::new(Expr::Lit(Lit::Str(Str {
                                        span: DUMMY_SP,
                                        value: "".into(),
                                        raw: None,
                                    }))),
                                }))
                            },
                            Type::TYPE_INT32 | Type::TYPE_INT64 | Type::TYPE_DOUBLE | Type::TYPE_FLOAT => {
                                Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Ident(quote_ident!("item"))),
                                        op: op!("!="),
                                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Ident(quote_ident!("item"))),
                                            op: op!("!=="),
                                            right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                        })),
                                        cons: Box::new(Expr::Cond(CondExpr {
                                            span: DUMMY_SP,
                                            test: Box::new(Expr::Bin(BinExpr {
                                                span: DUMMY_SP,
                                                left: Box::new(Expr::Unary(UnaryExpr {
                                                    span: DUMMY_SP,
                                                    op: op!("typeof"),
                                                    arg: Box::new(Expr::Ident(quote_ident!("item"))),
                                                })),
                                                op: op!("=="),
                                                right: Box::new(Expr::Lit(Lit::Str(Str {
                                                    span: DUMMY_SP,
                                                    value: "number".into(),
                                                    raw: None,
                                                }))),
                                            })),
                                            cons: Box::new(Expr::Ident(quote_ident!("item"))),
                                            alt: Box::new(Expr::Call(CallExpr {
                                                span: DUMMY_SP,
                                                callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("Number")))),
                                                args: vec![ExprOrSpread {
                                                    spread: None,
                                                    expr: Box::new(Expr::Ident(quote_ident!("item"))),
                                                }],
                                                type_args: None,
                                            })),
                                        })),
                                        alt: Box::new(Expr::Lit(Lit::Num(Number {
                                            span: DUMMY_SP,
                                            value: 0.0,
                                            raw: None,
                                        }))),
                                    })),
                                    alt: Box::new(Expr::Lit(Lit::Num(Number {
                                        span: DUMMY_SP,
                                        value: 0.0,
                                        raw: None,
                                    }))),
                                }))
                            },
                            Type::TYPE_BOOL => {
                                Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Ident(quote_ident!("item"))),
                                        op: op!("!="),
                                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Ident(quote_ident!("item"))),
                                            op: op!("!=="),
                                            right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                        })),
                                        cons: Box::new(Expr::Cond(CondExpr {
                                            span: DUMMY_SP,
                                            test: Box::new(Expr::Bin(BinExpr {
                                                span: DUMMY_SP,
                                                left: Box::new(Expr::Unary(UnaryExpr {
                                                    span: DUMMY_SP,
                                                    op: op!("typeof"),
                                                    arg: Box::new(Expr::Ident(quote_ident!("item"))),
                                                })),
                                                op: op!("=="),
                                                right: Box::new(Expr::Lit(Lit::Str(Str {
                                                    span: DUMMY_SP,
                                                    value: "boolean".into(),
                                                    raw: None,
                                                }))),
                                            })),
                                            cons: Box::new(Expr::Ident(quote_ident!("item"))),
                                            alt: Box::new(Expr::Call(CallExpr {
                                                span: DUMMY_SP,
                                                callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("Boolean")))),
                                                args: vec![ExprOrSpread {
                                                    spread: None,
                                                    expr: Box::new(Expr::Ident(quote_ident!("item"))),
                                                }],
                                                type_args: None,
                                            })),
                                        })),
                                        alt: Box::new(Expr::Lit(Lit::Bool(Bool {
                                            span: DUMMY_SP,
                                            value: false,
                                        }))),
                                    })),
                                    alt: Box::new(Expr::Lit(Lit::Bool(Bool {
                                        span: DUMMY_SP,
                                        value: false,
                                    }))),
                                }))
                            },
                            Type::TYPE_BYTES => {
                                Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Ident(quote_ident!("item"))),
                                        op: op!("!="),
                                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Ident(quote_ident!("item"))),
                                            op: op!("!=="),
                                            right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                        })),
                                        cons: Box::new(Expr::Cond(CondExpr {
                                            span: DUMMY_SP,
                                            test: Box::new(Expr::Bin(BinExpr {
                                                span: DUMMY_SP,
                                                left: Box::new(Expr::Unary(UnaryExpr {
                                                    span: DUMMY_SP,
                                                    op: op!("typeof"),
                                                    arg: Box::new(Expr::Ident(quote_ident!("item"))),
                                                })),
                                                op: op!("=="),
                                                right: Box::new(Expr::Lit(Lit::Str(Str {
                                                    span: DUMMY_SP,
                                                    value: "object".into(),
                                                    raw: None,
                                                }))),
                                            })),
                                            cons: Box::new(Expr::New(NewExpr {
                                                span: DUMMY_SP,
                                                callee: Box::new(Expr::Ident(quote_ident!("Uint8Array"))),
                                                args: Some(vec![ExprOrSpread {
                                                    spread: None,
                                                    expr: Box::new(Expr::Ident(quote_ident!("item"))),
                                                }]),
                                                type_args: None,
                                            })),
                                            alt: Box::new(Expr::New(NewExpr {
                                                span: DUMMY_SP,
                                                callee: Box::new(Expr::Ident(quote_ident!("Uint8Array"))),
                                                args: Some(vec![]),
                                                type_args: None,
                                            })),
                                        })),
                                        alt: Box::new(Expr::New(NewExpr {
                                            span: DUMMY_SP,
                                            callee: Box::new(Expr::Ident(quote_ident!("Uint8Array"))),
                                            args: Some(vec![]),
                                            type_args: None,
                                        })),
                                    })),
                                    alt: Box::new(Expr::New(NewExpr {
                                        span: DUMMY_SP,
                                        callee: Box::new(Expr::Ident(quote_ident!("Uint8Array"))),
                                        args: Some(vec![]),
                                        type_args: None,
                                    })),
                                }))
                            },
                            Type::TYPE_MESSAGE => {
                                let cleaned_type = field.type_name().trim_start_matches('.');
                                let type_parts: Vec<&str> = cleaned_type.split('.').collect();
                                Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Ident(quote_ident!("item"))),
                                        op: op!("!="),
                                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Ident(quote_ident!("item"))),
                                            op: op!("!=="),
                                            right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                        })),
                                        cons: Box::new(Expr::Call(CallExpr {
                                            span: DUMMY_SP,
                                            callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
                                                span: DUMMY_SP,
                                                obj: Box::new(Expr::Ident(quote_ident!(type_parts[0].to_string()))),
                                                prop: MemberProp::Ident(quote_ident!("fromObject")),
                                            }))),
                                            args: vec![ExprOrSpread {
                                                spread: None,
                                                expr: Box::new(Expr::Ident(quote_ident!("item"))),
                                            }],
                                            type_args: None,
                                        })),
                                        alt: Box::new(Expr::Call(CallExpr {
                                            span: DUMMY_SP,
                                            callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
                                                span: DUMMY_SP,
                                                obj: Box::new(Expr::Ident(quote_ident!(type_parts[0].to_string()))),
                                                prop: MemberProp::Ident(quote_ident!("default")),
                                            }))),
                                            args: vec![],
                                            type_args: None,
                                        })),
                                    })),
                                    alt: Box::new(Expr::Call(CallExpr {
                                        span: DUMMY_SP,
                                        callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!(type_parts[0].to_string()))),
                                            prop: MemberProp::Ident(quote_ident!("default")),
                                        }))),
                                        args: vec![],
                                        type_args: None,
                                    })),
                                }))
                            },
                            Type::TYPE_ENUM => {
                                let enum_name = field.type_name();
                                Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Ident(quote_ident!("item"))),
                                        op: op!("!="),
                                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Ident(quote_ident!("item"))),
                                            op: op!("!=="),
                                            right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                        })),
                                        cons: Box::new(Expr::Call(CallExpr {
                                            span: DUMMY_SP,
                                            callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
                                                span: DUMMY_SP,
                                                obj: Box::new(Expr::Ident(quote_ident!(enum_name))),
                                                prop: MemberProp::Ident(quote_ident!("fromValue")),
                                            }))),
                                            args: vec![ExprOrSpread {
                                                spread: None,
                                                expr: Box::new(Expr::Ident(quote_ident!("item"))),
                                            }],
                                            type_args: None,
                                        })),
                                        alt: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!(enum_name))),
                                            prop: MemberProp::Ident(quote_ident!("UNKNOWN")),
                                        })),
                                    })),
                                    alt: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!(enum_name))),
                                        prop: MemberProp::Ident(quote_ident!("UNKNOWN")),
                                    })),
                                }))
                            },
                            _ => Box::new(Expr::Ident(quote_ident!("item"))),
                        };
                
                        Box::new(Expr::Cond(CondExpr {
                            span: DUMMY_SP,
                            test: Box::new(Expr::Call(CallExpr {
                                span: DUMMY_SP,
                                callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
                                    span: DUMMY_SP,
                                    obj: Box::new(Expr::Ident(quote_ident!("Array"))),
                                    prop: MemberProp::Ident(quote_ident!("isArray")),
                                }))),
                                args: vec![ExprOrSpread {
                                    spread: None,
                                    expr: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                        prop: MemberProp::Ident(field_name_ident.clone()),
                                    })),
                                }],
                                type_args: None,
                            })),
                            cons: Box::new(Expr::Call(CallExpr {
                                span: DUMMY_SP,
                                callee: Callee::Expr(Box::new(Expr::Member(MemberExpr {
                                    span: DUMMY_SP,
                                    obj: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                        prop: MemberProp::Ident(field_name_ident.clone()),
                                    })),
                                    prop: MemberProp::Ident(quote_ident!("map")),
                                }))),
                                args: vec![ExprOrSpread {
                                    spread: None,
                                    expr: Box::new(Expr::Arrow(ArrowExpr {
                                        span: DUMMY_SP,
                                        params: vec![Pat::Ident(BindingIdent {
                                            id: quote_ident!("item"),
                                            type_ann:  Some(Box::new(TsTypeAnn {
                                                span: DUMMY_SP,
                                                type_ann: Box::new(match field.type_() {
                                                    Type::TYPE_STRING => TsType::TsKeywordType(TsKeywordType {
                                                        span: DUMMY_SP,
                                                        kind: TsKeywordTypeKind::TsStringKeyword,
                                                    }),
                                                    Type::TYPE_INT32 | Type::TYPE_INT64 | Type::TYPE_DOUBLE | Type::TYPE_FLOAT => 
                                                        TsType::TsKeywordType(TsKeywordType {
                                                            span: DUMMY_SP,
                                                            kind: TsKeywordTypeKind::TsNumberKeyword,
                                                        }),
                                                    Type::TYPE_BOOL => TsType::TsKeywordType(TsKeywordType {
                                                        span: DUMMY_SP,
                                                        kind: TsKeywordTypeKind::TsBooleanKeyword,
                                                    }),
                                                    Type::TYPE_BYTES => TsType::TsTypeRef(TsTypeRef {
                                                        span: DUMMY_SP,
                                                        type_name: TsEntityName::Ident(quote_ident!("Uint8Array")),
                                                        type_params: None,
                                                    }),
                                                    Type::TYPE_MESSAGE => TsType::TsTypeRef(TsTypeRef {
                                                        span: DUMMY_SP,
                                                        type_name: TsEntityName::Ident(quote_ident!(field.type_name())),
                                                        type_params: None,
                                                    }),
                                                    Type::TYPE_ENUM => TsType::TsTypeRef(TsTypeRef {
                                                        span: DUMMY_SP,
                                                        type_name: TsEntityName::Ident(quote_ident!(field.type_name())),
                                                        type_params: None,
                                                    }),
                                                    _ => TsType::TsKeywordType(TsKeywordType {
                                                        span: DUMMY_SP,
                                                        kind: TsKeywordTypeKind::TsAnyKeyword,
                                                    }),
                                                }),
                                            })),
                                        })],
                                        body: Box::new(BlockStmtOrExpr::Expr(element_conversion)),
                                        is_async: false,
                                        is_generator: false,
                                        type_params: None,
                                        return_type: None,
                                    })),
                                }],
                                type_args: None,
                            })),
                            alt: Box::new(Expr::Array(ArrayLit {
                                span: DUMMY_SP,
                                elems: vec![],
                            })),
                        }))
                    } else {//handle arrays
                        Box::new(Expr::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                            prop: MemberProp::Ident(field_name_ident.clone()),
                        }))
                    }
                    
                } else {
                    // Handle non-array fields
                    match field.type_() {
                        Type::TYPE_STRING => {
                            Box::new(Expr::Cond(CondExpr {
                                span: DUMMY_SP,
                                test: Box::new(Expr::Bin(BinExpr {
                                    span: DUMMY_SP,
                                    left: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                        prop: MemberProp::Ident(field_name_ident.clone()),
                                    })),
                                    op: op!("!="),
                                    right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                })),
                                cons: Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        op: op!("!=="),
                                        right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Unary(UnaryExpr {
                                                span: DUMMY_SP,
                                                op: op!("typeof"),
                                                arg: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            })),
                                            op: op!("=="),
                                            right: Box::new(Expr::Lit(Lit::Str(Str {
                                                span: DUMMY_SP,
                                                value: "string".into(),
                                                raw: None,
                                            }))),
                                        })),
                                        cons: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        alt: Box::new(Expr::Call(CallExpr {
                                            span: DUMMY_SP,
                                            callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("String")))),
                                            args: vec![ExprOrSpread {
                                                spread: None,
                                                expr: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            }],
                                            type_args: None,
                                        })),
                                    })),
                                    alt: Box::new(Expr::Lit(Lit::Str(Str {
                                        span: DUMMY_SP,
                                        value: "".into(),
                                        raw: None,
                                    }))),
                                })),
                                alt: Box::new(Expr::Lit(Lit::Str(Str {
                                    span: DUMMY_SP,
                                    value: "".into(),
                                    raw: None,
                                }))),
                            }))
                        },
                        Type::TYPE_INT32 | Type::TYPE_INT64 | Type::TYPE_DOUBLE | Type::TYPE_FLOAT => {
                            Box::new(Expr::Cond(CondExpr {
                                span: DUMMY_SP,
                                test: Box::new(Expr::Bin(BinExpr {
                                    span: DUMMY_SP,
                                    left: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                        prop: MemberProp::Ident(field_name_ident.clone()),
                                    })),
                                    op: op!("!="),
                                    right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                })),
                                cons: Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        op: op!("!=="),
                                        right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Unary(UnaryExpr {
                                                span: DUMMY_SP,
                                                op: op!("typeof"),
                                                arg: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            })),
                                            op: op!("=="),
                                            right: Box::new(Expr::Lit(Lit::Str(Str {
                                                span: DUMMY_SP,
                                                value: "number".into(),
                                                raw: None,
                                            }))),
                                        })),
                                        cons: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        alt: Box::new(Expr::Call(CallExpr {
                                            span: DUMMY_SP,
                                            callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("Number")))),
                                            args: vec![ExprOrSpread {
                                                spread: None,
                                                expr: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            }],
                                            type_args: None,
                                        })),
                                    })),
                                    alt: Box::new(Expr::Lit(Lit::Num(Number {
                                        span: DUMMY_SP,
                                        value: 0.0,
                                        raw: None,
                                    }))),
                                })),
                                alt: Box::new(Expr::Lit(Lit::Num(Number {
                                    span: DUMMY_SP,
                                    value: 0.0,
                                    raw: None,
                                }))),
                            }))
                        },
                        Type::TYPE_BOOL => {
                            Box::new(Expr::Cond(CondExpr {
                                span: DUMMY_SP,
                                test: Box::new(Expr::Bin(BinExpr {
                                    span: DUMMY_SP,
                                    left: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                        prop: MemberProp::Ident(field_name_ident.clone()),
                                    })),
                                    op: op!("!="),
                                    right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                })),
                                cons: Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        op: op!("!=="),
                                        right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Unary(UnaryExpr {
                                                span: DUMMY_SP,
                                                op: op!("typeof"),
                                                arg: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            })),
                                            op: op!("=="),
                                            right: Box::new(Expr::Lit(Lit::Str(Str {
                                                span: DUMMY_SP,
                                                value: "boolean".into(),
                                                raw: None,
                                            }))),
                                        })),
                                        cons: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        alt: Box::new(Expr::Call(CallExpr {
                                            span: DUMMY_SP,
                                            callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("Boolean")))),
                                            args: vec![ExprOrSpread {
                                                spread: None,
                                                expr: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            }],
                                            type_args: None,
                                        })),
                                    })),
                                    alt: Box::new(Expr::Lit(Lit::Bool(Bool {
                                        span: DUMMY_SP,
                                        value: false,
                                    }))),
                                })),
                                alt: Box::new(Expr::Lit(Lit::Bool(Bool {
                                    span: DUMMY_SP,
                                    value: false,
                                }))),
                            }))
                        },
                        Type::TYPE_BYTES => {
                            Box::new(Expr::Cond(CondExpr {
                                span: DUMMY_SP,
                                test: Box::new(Expr::Bin(BinExpr {
                                    span: DUMMY_SP,
                                    left: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                        prop: MemberProp::Ident(field_name_ident.clone()),
                                    })),
                                    op: op!("!="),
                                    right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                })),
                                cons: Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        op: op!("!=="),
                                        right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                    })),
                                    cons: Box::new(Expr::New(NewExpr {
                                        span: DUMMY_SP,
                                        callee: Box::new(Expr::Ident(quote_ident!("Uint8Array"))),
                                        args: Some(vec![ExprOrSpread {
                                            spread: None,
                                            expr: Box::new(Expr::Member(MemberExpr {
                                                span: DUMMY_SP,
                                                obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                prop: MemberProp::Ident(field_name_ident.clone()),
                                            })),
                                        }]),
                                        type_args: None,
                                    })),
                                    alt: Box::new(Expr::New(NewExpr {
                                        span: DUMMY_SP,
                                        callee: Box::new(Expr::Ident(quote_ident!("Uint8Array"))),
                                        args: Some(vec![]),
                                        type_args: None,
                                    })),
                                })),
                                alt: Box::new(Expr::New(NewExpr {
                                    span: DUMMY_SP,
                                    callee: Box::new(Expr::Ident(quote_ident!("Uint8Array"))),
                                    args: Some(vec![]),
                                    type_args: None,
                                })),
                            }))
                        },
                        Type::TYPE_MESSAGE => {
                            Box::new(Expr::Member(MemberExpr {
                                span: DUMMY_SP,
                                obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                prop: MemberProp::Ident(field_name_ident.clone()),
                            }))
                        },
                        Type::TYPE_ENUM => {
                            Box::new(Expr::Cond(CondExpr {
                                span: DUMMY_SP,
                                test: Box::new(Expr::Bin(BinExpr {
                                    span: DUMMY_SP,
                                    left: Box::new(Expr::Member(MemberExpr {
                                        span: DUMMY_SP,
                                        obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                        prop: MemberProp::Ident(field_name_ident.clone()),
                                    })),
                                    op: op!("!="),
                                    right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                                })),
                                cons: Box::new(Expr::Cond(CondExpr {
                                    span: DUMMY_SP,
                                    test: Box::new(Expr::Bin(BinExpr {
                                        span: DUMMY_SP,
                                        left: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        op: op!("!=="),
                                        right: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
                                    })),
                                    cons: Box::new(Expr::Cond(CondExpr {
                                        span: DUMMY_SP,
                                        test: Box::new(Expr::Bin(BinExpr {
                                            span: DUMMY_SP,
                                            left: Box::new(Expr::Unary(UnaryExpr {
                                                span: DUMMY_SP,
                                                op: op!("typeof"),
                                                arg: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            })),
                                            op: op!("=="),
                                            right: Box::new(Expr::Lit(Lit::Str(Str {
                                                span: DUMMY_SP,
                                                value: "number".into(),
                                                raw: None,
                                            }))),
                                        })),
                                        cons: Box::new(Expr::Member(MemberExpr {
                                            span: DUMMY_SP,
                                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                            prop: MemberProp::Ident(field_name_ident.clone()),
                                        })),
                                        alt: Box::new(Expr::Call(CallExpr {
                                            span: DUMMY_SP,
                                            callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("Number")))),
                                            args: vec![ExprOrSpread {
                                                spread: None,
                                                expr: Box::new(Expr::Member(MemberExpr {
                                                    span: DUMMY_SP,
                                                    obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                                                    prop: MemberProp::Ident(field_name_ident.clone()),
                                                })),
                                            }],
                                            type_args: None,
                                        })),
                                    })),
                                    alt: Box::new(Expr::Lit(Lit::Num(Number {
                                        span: DUMMY_SP,
                                        value: 0.0,
                                        raw: None,
                                    }))),
                                })),
                                alt: Box::new(Expr::Lit(Lit::Num(Number {
                                    span: DUMMY_SP,
                                    value: 0.0,
                                    raw: None,
                                }))),
                            }))
                        },
                        _ => Box::new(Expr::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("obj"))),
                            prop: MemberProp::Ident(field_name_ident.clone()),
                        })),
                    }
                };
                props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
                    key: PropName::Ident(field_name_ident.clone()),
                    value: conversion_expr,
                }))));
            }
        }

        
    
        // 创建 fromObject 方法
        let from_object_method = ClassMethod {
            span: DUMMY_SP,
            key: PropName::Ident(quote_ident!("fromObject")),
            function: Box::new(swc_ecma_ast::Function {
                params: vec![Param {
                    span: DUMMY_SP,
                    decorators: vec![],
                    pat: Pat::Ident(BindingIdent {
                        id: quote_ident!("obj"),
                        type_ann: Some(Box::new(TsTypeAnn {
                            span: DUMMY_SP,
                            type_ann: Box::new(TsType::TsTypeRef(TsTypeRef {
                                span: DUMMY_SP,
                                type_name: TsEntityName::Ident(quote_ident!("Record")),
                                type_params: Some(Box::new(TsTypeParamInstantiation {
                                    span: DUMMY_SP,
                                    params: vec![
                                        Box::new(TsType::TsKeywordType(TsKeywordType {
                                            span: DUMMY_SP,
                                            kind: TsKeywordTypeKind::TsStringKeyword,
                                        })),
                                        Box::new(TsType::TsTypeRef(TsTypeRef {
                                            span: DUMMY_SP,
                                            type_name: TsEntityName::Ident(quote_ident!("ESObject")),
                                            type_params: None,
                                        })),
                                    ],
                                })),
                            })),
                        })),
                    }),
                }],
                decorators: vec![],
                span: DUMMY_SP,
                body: Some(swc_ecma_ast::BlockStmt {
                    span: DUMMY_SP,
                    stmts: vec![
                        // 创建 adapted 对象
                        Stmt::Decl(Decl::Var(Box::new(VarDecl {
                            span: DUMMY_SP,
                            kind: VarDeclKind::Const,
                            decls: vec![VarDeclarator {
                                span: DUMMY_SP,
                                name: Pat::Ident(BindingIdent {
                                    id: quote_ident!("adapted"),
                                    type_ann: Some(Box::new(TsTypeAnn {
                                        span: DUMMY_SP,
                                        type_ann: Box::new(TsType::TsTypeRef(TsTypeRef {
                                            span: DUMMY_SP,
                                            type_name: TsEntityName::Ident(interface_type.clone()),
                                            type_params: None,
                                        })),
                                    })),  // 如果没有类型注解可以设为 None
                                }),
                                init: Some(Box::new(Expr::Object(ObjectLit {
                                    span: DUMMY_SP,
                                    props,
                                }))),
                                definite: false,
                            }],
                            declare: false,
                        }))),
                        // 返回创建的对象
                        crate::return_stmt!(crate::call_expr!(
                            crate::member_expr!(instance_type.clone(), "create"),
                            vec![crate::expr_or_spread!(Expr::Ident(quote_ident!("adapted")))]
                        )),
                    ],
                }),
                is_generator: false,
                is_async: false,
                type_params: None,
                return_type: Some(Box::new(crate::type_annotation!(crate::type_ref!(
                    instance_type.clone().into()
                )))),
            }),
            kind: MethodKind::Method,
            is_static: false,
            accessibility: Some(Accessibility::Public),
            is_abstract: false,
            is_optional: false,
            is_override: false,
        };
    
        // 创建类定义
        let class = Class {
            span: DUMMY_SP,
            decorators: vec![],
            body: vec![ClassMember::Method(from_object_method)],
            super_class: None,
            is_abstract: false,
            type_params: None,
            super_type_params: None,
            implements: vec![implement],
        };
    
        ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
            span: DUMMY_SP,
            decl: Decl::Class(swc_ecma_ast::ClassDecl {
                ident: quote_ident!("$FromObject"),
                declare: false,
                class: Box::new(class),
            }),
        }))
    }

    // 判断字段是否是 map 类型的辅助函数
    pub fn is_map_field(field: &FieldDescriptorProto) -> bool {
        // Protobuf 中 map 类型的特征检查
        field.type_() == Type::TYPE_MESSAGE &&
        field.type_name().ends_with("Entry")  // map 类型通常以 "Entry" 结尾
    }


    pub(super) fn print_to_object(
        &self,
        ctx: &mut Context,
        descriptor: &DescriptorProto,
        ns: &[String],
    ) -> ModuleItem {
        let lark = ctx.get_import(Self::LARK_PACKAGE, Some(Self::LARK_PACKAGE_NAME));
        let interface_type = ctx.lazy_decl_type_ref(&ns_name(ns, descriptor.name()), PropPos::Interface);
        let instance_type = ctx.lazy_decl_type_ref(&ns_name(ns, descriptor.name()), PropPos::Definition);
    
        // Implement ToObject interface
        let implement = TsExprWithTypeArgs {
            span: DUMMY_SP,
            expr: Box::new(crate::member_expr!(lark, "ToObject")),
            type_args: Some(Box::new(swc_ecma_ast::TsTypeParamInstantiation {
                span: DUMMY_SP,
                params: vec![
                    Box::new(TsType::TsTypeRef(TsTypeRef {
                        span: DUMMY_SP,
                        type_name: TsEntityName::Ident(interface_type.clone()),
                        type_params: None,
                    })),
                    Box::new(TsType::TsTypeRef(TsTypeRef {
                        span: DUMMY_SP,
                        type_name: TsEntityName::Ident(instance_type.clone()),
                        type_params: None,
                    })),
                ],
            })),
        };
    
        // Create return type Record<string, ESObject>
        let record_type = TsType::TsTypeRef(TsTypeRef {
            span: DUMMY_SP,
            type_name: TsEntityName::Ident(quote_ident!("Record")),
            type_params: Some(Box::new(TsTypeParamInstantiation {
                span: DUMMY_SP,
                params: vec![
                    Box::new(TsType::TsKeywordType(TsKeywordType {
                        span: DUMMY_SP,
                        kind: TsKeywordTypeKind::TsStringKeyword,
                    })),
                    Box::new(TsType::TsTypeRef(TsTypeRef {
                        span: DUMMY_SP,
                        type_name: TsEntityName::Ident(quote_ident!("ESObject")),
                        type_params: None,
                    })),
                ],
            })),
        });
    
        // Create method body
        let mut stmts = Vec::new();
        
        // Add result variable declaration
        // 修改 result 变量声明部分
        stmts.push(Stmt::Decl(Decl::Var(Box::new(VarDecl {
            span: DUMMY_SP,
            kind: VarDeclKind::Let,
            decls: vec![VarDeclarator {
                span: DUMMY_SP,
                name: Pat::Ident(BindingIdent {
                    id: quote_ident!("result"),
                    type_ann: Some(Box::new(TsTypeAnn {
                        span: DUMMY_SP,
                        type_ann: Box::new(TsType::TsTypeRef(TsTypeRef {
                            span: DUMMY_SP,
                            type_name: TsEntityName::Ident(quote_ident!("Record")),
                            type_params: Some(Box::new(TsTypeParamInstantiation {
                                span: DUMMY_SP,
                                params: vec![
                                    Box::new(TsType::TsKeywordType(TsKeywordType {
                                        span: DUMMY_SP,
                                        kind: TsKeywordTypeKind::TsStringKeyword,
                                    })),
                                    Box::new(TsType::TsTypeRef(TsTypeRef {
                                        span: DUMMY_SP,
                                        type_name: TsEntityName::Ident(quote_ident!("ESObject")),
                                        type_params: None,
                                    })),
                                ],
                            })),
                        })),
                    })),
                }.into()),
                init: Some(Box::new(Expr::Object(ObjectLit {
                    span: DUMMY_SP,
                    props: vec![],
                }))),
                definite: false,
            }],
            declare: false,
        }))));

        //handle oneof
        for oneof in &descriptor.oneof_decl {//处理oneof字段
            let field_name = normalize_name(oneof.name());
            let field_ident = quote_ident!(field_name.clone());
            
            // 构建条件语句
            let if_stmt = Stmt::If(IfStmt {
                span: DUMMY_SP,
                test: Box::new(Expr::Bin(BinExpr {
                    span: DUMMY_SP,
                    op: BinaryOp::LogicalAnd,
                    left: Box::new(Expr::Bin(BinExpr {
                        span: DUMMY_SP,
                        op: BinaryOp::NotEqEq,
                        left: Box::new(Expr::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("instance"))),
                            prop: MemberProp::Ident(field_ident.clone()),
                        })),
                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                    })),
                    right: Box::new(Expr::Bin(BinExpr {
                        span: DUMMY_SP,
                        op: BinaryOp::NotEqEq,
                        left: Box::new(Expr::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("instance"))),
                            prop: MemberProp::Ident(field_ident),
                        })),
                        right: Box::new(Expr::Ident(quote_ident!("null"))),
                    })),
                })),

                cons: Box::new(Stmt::Expr(ExprStmt {
                    span: DUMMY_SP,
                    expr: Box::new(Expr::Assign(AssignExpr {
                        span: DUMMY_SP,
                        op: AssignOp::Assign,
                        left: AssignTarget::Simple(SimpleAssignTarget::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("result"))),
                            prop: MemberProp::Computed(ComputedPropName {
                                span: DUMMY_SP,
                                expr: Box::new(Expr::Lit(Lit::Str(Str {
                                    span: DUMMY_SP,
                                    value: field_name.clone().into(),
                                    raw: None,
                                }))),
                            }),
                        })),
                        right: Box::new(Expr::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("instance"))),
                            prop: MemberProp::Ident(quote_ident!(field_name)),
                        })),
                    })),
                })),
                alt: None,
            });

            stmts.push(if_stmt);
        }
            
        // Add conditional assignments for each field
        for field in &descriptor.field {
            let field_name = field.name();
            let camel_case_name = quote_ident!(normalize_name(field_name));
            if !field.has_oneof_index() {//handle no oneof
                let condition = Box::new(Expr::Bin(BinExpr {
                    span: DUMMY_SP,
                    op: BinaryOp::LogicalAnd,
                    left: Box::new(Expr::Bin(BinExpr {
                        span: DUMMY_SP,
                        op: BinaryOp::NotEq,
                        left: Box::new(Expr::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("instance"))),
                            prop: MemberProp::Ident(camel_case_name.clone()),
                        })),
                        right: Box::new(Expr::Ident(quote_ident!("undefined"))),
                    })),
                    right: Box::new(Expr::Bin(BinExpr {
                        span: DUMMY_SP,
                        op: BinaryOp::NotEq,
                        left: Box::new(Expr::Member(MemberExpr {
                            span: DUMMY_SP,
                            obj: Box::new(Expr::Ident(quote_ident!("instance"))),
                            prop: MemberProp::Ident(camel_case_name.clone()),
                        })),
                        right: Box::new(Expr::Ident(quote_ident!("null"))),
                    })),
                }));
        
                // Create assignment using simple pattern
                let assignment = Stmt::If(IfStmt {
                    span: DUMMY_SP,
                    test: condition,
                    cons: Box::new(Stmt::Expr(ExprStmt {
                        span: DUMMY_SP,
                        expr: Box::new(Expr::Assign(AssignExpr {
                            span: DUMMY_SP,
                            op: AssignOp::Assign,
                            left: AssignTarget::Simple(SimpleAssignTarget::Member(MemberExpr {
                                span: DUMMY_SP,
                                obj: Box::new(Expr::Ident(quote_ident!("result"))),
                                prop: MemberProp::Computed(ComputedPropName {
                                    span: DUMMY_SP,
                                    expr: Box::new(Expr::Lit(Lit::Str(Str {
                                        span: DUMMY_SP,
                                        value: field_name.into(),
                                        raw: None,
                                    }))),
                                }),
                            })),
                            right: Box::new(Expr::Member(MemberExpr {
                                span: DUMMY_SP,
                                obj: Box::new(Expr::Ident(quote_ident!("instance"))),
                                prop: MemberProp::Ident(camel_case_name.clone()),
                            })),
                        })),
                    })),
                    alt: None,
                });
                stmts.push(assignment);
            }
            
        }
    
        // Add return statement
        stmts.push(Stmt::Return(ReturnStmt {
            span: DUMMY_SP,
            arg: Some(Box::new(Expr::Ident(quote_ident!("result")))),
        }));
    
        // Create toObject method
        let to_object_method = ClassMethod {
            span: DUMMY_SP,
            key: PropName::Ident(quote_ident!("toObject")),
            function: Box::new(Function {
                params: vec![Param {
                    span: DUMMY_SP,
                    decorators: vec![],
                    pat: crate::pat_ident!(
                        quote_ident!("instance"),
                        crate::type_annotation!(crate::type_ref!(interface_type.clone().into()))
                    ),
                }],
                decorators: vec![],
                span: DUMMY_SP,
                body: Some(BlockStmt {
                    span: DUMMY_SP,
                    stmts,
                }),
                is_generator: false,
                is_async: false,
                type_params: None,
                return_type: Some(Box::new(crate::type_annotation!(record_type))),
            }),
            kind: MethodKind::Method,
            is_static: false,
            accessibility: Some(Accessibility::Public),
            is_abstract: false,
            is_optional: false,
            is_override: false,
        };
    
        // Create class definition
        let class = Class {
            span: DUMMY_SP,
            decorators: vec![],
            body: vec![ClassMember::Method(to_object_method)],
            super_class: None,
            is_abstract: false,
            type_params: None,
            super_type_params: None,
            implements: vec![implement],
        };
    
        ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
            span: DUMMY_SP,
            decl: Decl::Class(ClassDecl {
                ident: quote_ident!("$ToObject"),
                declare: false,
                class: Box::new(class),
            }),
        }))
    }
}